﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Bll.IObjectBlls;
using CSP.Bll;
using CSP.Model.CSP;
using System.Collections.ObjectModel;
using CSP.Common.Web;
using Newtonsoft.Json;

namespace CSP.Web.MyHome.ServiceInfo
{
    public partial class ServiceType : CSPMyHome
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (Request["parentid"] != null)
                {
                    int parentId = Convert.ToInt32(Request.QueryString["parentid"]);
                    string serviceTypeJson = GetServiceTypeJsonByParentId(parentId);
                    Response.ClearContent();
                    Response.Write(serviceTypeJson);
                    Response.ContentType = "text/plain";
                    Response.End();
                }
                else if (Request["parentsubid"] != null)
                {
                    int parentSubId = Convert.ToInt32(Request.QueryString["parentsubid"]);
                    string serviceTypeJson = GetServiceTypeSubByParentId(parentSubId);
                    Response.ClearContent();
                    Response.Write(serviceTypeJson);
                    Response.ContentType = "text/plain";
                    Response.End();
                }
                else if (Request["CheckContactCount"] != null)
                {
                    int contactCount = Convert.ToInt32(GetCountByUser());
                    string checkContactCountJson = "";
                    if (contactCount == 0)
                    {
                        checkContactCountJson = "{\"status\":0}";
                    }
                    else
                    {
                        checkContactCountJson = "{\"status\":1}";
                    }
                    Response.Clear();
                    Response.Write(checkContactCountJson);
                    Response.End();
                }
                else if (Request["domainType"] != null)
                {
                    
                    string serviceTypeJson = GetDomainTypeJson();
                    Response.ClearContent();
                    Response.Write(serviceTypeJson);
                    Response.ContentType = "text/plain";
                    Response.End();
                }
            }
        }


        /// <summary>
        /// 绑定领域类型
        /// </summary>
        private List<ServiceTypeEntity> GetDomainType()
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            ICollection<CSP.Model.CSP.ServiceType> serviceTypes = null;
            using (IServiceTypeBll serviceTypeBll = bllFactory.GetServiceTypeBll())
            {
                serviceTypes = serviceTypeBll.FindAllByAvailable();
            }
            List<Model.CSP.ServiceType> domainType = serviceTypes.Where(s => s.ParentId == 0).OrderBy(s => s.OrderId).ToList();
            List<ServiceTypeEntity> result = (from serviceType in domainType
                                              select new ServiceTypeEntity
                                              {
                                                  Id = serviceType.Id,
                                                  Name = serviceType.Name,
                                                  ParentId = serviceType.ParentId
                                              }).ToList();
            return result;
        }

        /// <summary>
        /// 获取领域类型的字符串
        /// </summary>
        /// <returns></returns>
        private string GetDomainTypeJson()
        {
            List<ServiceTypeEntity> serviceTypeEntities = GetDomainType();
            return JsonConvert.SerializeObject(serviceTypeEntities);
        }

        /// <summary>
        /// 绑定服务类型
        /// </summary>
        /// <param name="parentId"></param>
        private string GetServiceTypeJsonByParentId(int parentId)
        {
            List<ServiceTypeEntity> serviceTypeEntities = this.GetServiceTypeEntitiesByParentId(parentId);
            return JsonConvert.SerializeObject(serviceTypeEntities);
        }

        /// <summary>
        /// 绑定子服务域名
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private string GetServiceTypeSubByParentId(int parentId)
        {

            List<ServiceTypeEntity> serviceTypeEntities = this.GetServiceTypeEntitiesByParentId(parentId);
            return JsonConvert.SerializeObject(serviceTypeEntities);
        }


        /// <summary>
        /// 从Bll层根据ParentId获取ServiceTypes
        /// </summary>
        /// <returns></returns>
        private ICollection<CSP.Model.CSP.ServiceType> FindServiceTypesByParentIdAndBll()
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            ICollection<CSP.Model.CSP.ServiceType> serviceTypes = null;
            using (IServiceTypeBll serviceTypeBll = bllFactory.GetServiceTypeBll())
            {
                serviceTypes = serviceTypeBll.FindAllByAvailable();
            }
            return serviceTypes;
        }

        /// <summary>
        /// 根据parentId获取ServiceTypeEntities 数组
        /// </summary>
        /// <param name="parentId">fuID</param>
        /// <returns></returns>
        private List<ServiceTypeEntity> GetServiceTypeEntitiesByParentId(int parentId)
        {
            ICollection<CSP.Model.CSP.ServiceType> serviceTypes = FindServiceTypesByParentIdAndBll();
            serviceTypes = serviceTypes.Where(s => s.ParentId == parentId).OrderBy(s => s.OrderId).ToList();

            List<ServiceTypeEntity> result = (from serviceType in serviceTypes
                                              select new ServiceTypeEntity
                                              {
                                                  Id = serviceType.Id,
                                                  Name = serviceType.Name,
                                                  ParentId = serviceType.ParentId
                                              }).ToList();

            return result;
        }

        /// <summary>
        /// 递归树形排序
        /// </summary>
        /// <param name="serviceTypes"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        [Obsolete("此方法已弃用", true)]
        private List<ServiceTypeEntity> GetServiceTypesForUI(ICollection<CSP.Model.CSP.ServiceType> serviceTypes, int parentId)
        {
            List<ServiceTypeEntity> result = null;
            if (serviceTypes == null)
            {
                return result;
            }

            //查询出来当parentId的数据
            var temServiceTypes = serviceTypes.Where(s => s.ParentId == parentId).OrderBy(s => s.OrderId);
            if (temServiceTypes.Count() == 0)
            {
                return result;
            }

            result = new List<ServiceTypeEntity>();

            //对查询出来的parentId进行递归取出其下的数据
            foreach (CSP.Model.CSP.ServiceType serviceType in temServiceTypes)
            {
                ServiceTypeEntity serviceTypeEntity = new ServiceTypeEntity
                {
                    Id = serviceType.Id,
                    Name = serviceType.Name,
                    ParentId = serviceType.ParentId
                };
                if (serviceType.Level == 2)
                {
                    serviceTypeEntity.Name = "—" + serviceType.Name + "";
                }
                else if (serviceType.Level == 3)
                {
                    serviceTypeEntity.Name = "&nbsp;&nbsp;&nbsp;&nbsp;" + serviceType.Name + "";
                }
                result.Add(serviceTypeEntity);

                List<ServiceTypeEntity> nextServiceTypes = GetServiceTypesForUI(serviceTypes, serviceType.Id);
                if (nextServiceTypes != null)
                {
                    result.AddRange(nextServiceTypes);
                }
            }

            return result;
        }

        /// <summary>
        /// 根据用户名获取数量
        /// </summary>
        /// <returns></returns>
        private int GetCountByUser()
        {
            int result = 0;
            using (IContactWayBll _contactWayBll = CSPBllFactory.GetInstance().GetContactWayBll())
            {
                result = _contactWayBll.GetCountByUser(UserProvider.UserName);
            }

            return result;
        }

        private class ServiceTypeEntity
        {
            public int Id { get; set; }

            public string Name { get; set; }

            public int ParentId { get; set; }
        }

    }
}